/*
 * Copyright (C) 2008
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.sca4idea.providers.impls;

import com.googlecode.sca4idea.providers.SCAComponentImplementation;
import com.googlecode.sca4idea.references.SCDLResourceReferenceProvider;
import com.googlecode.sca4idea.references.SCDLLocationReferenceProvider;
import com.intellij.psi.PsiReferenceProvider;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.psi.xml.XmlTag;

/**
 * SCA Component Implementation for components backed by Java classes.
 */
public class CompositeComponentImplementation implements SCAComponentImplementation {
    private final String altAttrName;
    private final boolean isResource;
    private SCDLResourceReferenceProvider resourceProvider = new SCDLResourceReferenceProvider();
    private SCDLLocationReferenceProvider locationProvider = new SCDLLocationReferenceProvider();

    public CompositeComponentImplementation() {
        altAttrName = null;
        isResource = false;
    }

    public CompositeComponentImplementation(String altAttrName, boolean isResource) {
        this.altAttrName = altAttrName;
        this.isResource = isResource;
    }

    public PsiReferenceProvider getReferenceProvider(String attrName, XmlAttributeValue attrVal) {
        XmlTag tag = (XmlTag) attrVal.getParent().getParent();

        // Check if there is an alternate attribute name
        if (altAttrName != null) {
            if (!attrName.equals(altAttrName)) {
                return null;
            }

            if (this.isResource) {
                return resourceProvider;
            } else {
                return locationProvider;
            }
        } else {
            if (attrName.equals("scdlResource")) {
                return resourceProvider;
            } else if (attrName.equals("scdlLocation")) {
                return locationProvider;
            } else {
                return null;
            }
        }
    }
}